package springair.xml 
{
	import springair.domain.swc.RSLSWC;
	import springair.domain.swc.digest.RSLDefinition;
	import springair.domain.swc.digest.RSLDependancy;
	import springair.domain.swc.digest.RSLFeatures;
	import springair.domain.swc.digest.RSLFiles;
	import springair.domain.swc.digest.RSLLibrary;
	import springair.domain.swc.digest.RSLScript;
	import springair.domain.swc.digest.RSLVersions;

	/**
	 * @author a.fell
	 */
	public class RSLDigestParser 
	{
		
		private const VERSIONS_TAG:String = 'versions';
		private const FEATURES_TAG:String = 'features';
		private const SCRIPT_TAG:String = 'script';
		
		public function parseXML( xml : XML ) : *
		{
			var swc : RSLSWC = new RSLSWC;
			
			swc.versions = _parseVersions(xml.descendants(VERSIONS_TAG)[0]);
			swc.features = _parseFeatures(xml.descendants(FEATURES_TAG)[0]);
			
			swc.libraries = _parseLibraries(xml.descendants('libraries')[0]);
			swc.files = _parseFiles(xml.descendants('files')[0]);
			
			return swc;
		}

		protected function _parseFiles(filesXML : XML) : RSLFiles
		{
			var files : RSLFiles = new RSLFiles;
			
			return files;
		}

		protected function _parseLibraries(librariesXML : XML) : Array
		{
			var libraries : Array = [];
			
			for each (var libraryNode : XML in librariesXML.children()) 
			{
				var library : RSLLibrary = _parseLibrary(libraryNode);
				libraries.push(library);
			}
			
			return libraries;
		}
		
		protected function _parseLibrary(libraryXML : XML) : RSLLibrary
		{
			var library : RSLLibrary = new RSLLibrary;
			library.path = libraryXML.@path;
			
			for each (var scriptNode : XML in libraryXML.elements(SCRIPT_TAG)) 
			{
				var script : RSLScript = _parseScript(scriptNode);
				library.scripts.push(script);
			}
			
			return library;
		}
		
		protected function _parseScript(scriptNode : XML) : RSLScript
		{
			var script : RSLScript = new RSLScript;
			script.name = scriptNode.@name;
			script.mod = scriptNode.@mod;
			script.definition = _parseRSLDefinition(scriptNode.elements('def')[0]);
			
			for each (var dependancyNode : XML in scriptNode.elements('dep')) 
			{
				var dependancy : RSLDependancy = _parseRSLDependancy(dependancyNode);
				script.dependancies.push(dependancy);
			}
			
			return script;
		}
		
		protected function _parseRSLDependancy(dependancyNode : XML) : RSLDependancy
		{
			var dependancy : RSLDependancy = new RSLDependancy;
			
			dependancy.id = dependancyNode.@id;
			
			dependancy.type = dependancyNode.@type;
			
			return dependancy;
		}

		protected function _parseRSLDefinition(definitionXML:XML) : RSLDefinition
		{
			var definition : RSLDefinition = new RSLDefinition;
			definition.id = definitionXML.@id;
			return definition;
		}

		protected function _parseVersions(versionsXML : XML) : RSLVersions
		{
			var versions : RSLVersions = new RSLVersions;
			return versions;
		}

		protected function _parseFeatures(featuresXML : XML) : RSLFeatures
		{
			var features : RSLFeatures = new RSLFeatures;
			trace(featuresXML);
			return features;
		}
	}
}
